<?php

namespace App\Exports;

use App\Models\HousingsNum;
use App\Models\Report;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;


class HousingsNumExport implements FromCollection,WithHeadings,WithMapping
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $query=HousingsNum::query()->with('housings');
        if (isset($this->filters['ids'])) {
            $query->whereIn('id', explode(',',$this->filters['ids']));
        }
        return $query->get();
    }


    public function headings(): array
    {
        return [
            '房源码',
            '房源状态',
            '房源码颜色',
            '房屋地址',
            '经营负责人',
            '数据更新时间',
            '数据推送时间',
            // ... 其他列标题
        ];
    }

    public function map($housingsNum): array
    {
        // 假设订单有 id 和 total 字段
        $housings = $housingsNum->housings;

        $orderData[] = [
            $housingsNum['housings_num'],
            $housingsNum['type']=isset($housings->type) ? $housings->type :"",
            $housingsNum['colour'],
            $housingsNum['hou_address']=isset($housings->hou_address) ? $housings->hou_address :'',
            $housingsNum['landlord_name']=isset($housings->landlord_name) ? $housings->landlord_name :"",
            "",
            ""
        ];
        // 展开订单数据，以便每行都有用户信息和对应的订单信息
        return array_merge(...$orderData);
    }
}
